﻿using System;
using fastCSharp.code.cSharp;
using System.Threading;

namespace diantou.web.enterprise
{
    /// <summary>
    /// 长连接轮询
    /// </summary>
    [fastCSharp.code.cSharp.webView(IsPool = true, IsPage = false)]
    partial class poll : web.poll<poll>
    {
        /// <summary>
        /// 长连接轮询信息
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        [fastCSharp.code.cSharp.webView.outputAjax(IsIgnoreNull = true)]
        private diantou.examDataAccess.enterprisePoll value;
        /// <summary>
        /// 轮询回调处理
        /// </summary>
        private Action<fastCSharp.net.returnValue<diantou.examDataAccess.enterprisePoll>> onMessageHandle;
        /// <summary>
        /// 轮询回调处理
        /// </summary>
        /// <param name="value"></param>
        private void onMessage(fastCSharp.net.returnValue<diantou.examDataAccess.enterprisePoll> value)
        {
            Interlocked.Decrement(ref web.poll.AsynchronousCount);
            if (value.Type == fastCSharp.net.returnValue.type.Success && value.Value != null)
            {
                this.value = value.Value;
                callback();
            }
            else if (ifNoneMatch) notChanged304();
            else
            {
                fastCSharp.net.tcp.http.response response = cancelAsynchronous();
                response.SetETag(web.poll.NullETag);
                callback(ref response);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="verify"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        private bool loadView(fastCSharp.web.poll.verifyInfo verify, diantou.examDataAccess.enterprisePoll.query query)
        {
            web.poll.SecondCount.Add();
            if (web.poll.Poll.Verify(ref verify))
            {
                if (onMessageHandle == null) onMessageHandle = onMessage;
                ifNoneMatch = requestHeader.IfNoneMatch.Count != 0;
                isVerify = true;
                setAsynchronous();
                Interlocked.Increment(ref web.poll.AsynchronousCount);
                diantou.examDataAccess.tcpCall.enterprisePoll.get(verify.UserId, query, onMessageHandle);
            }
            return true;
        }
    }
}
